home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld Secrets (4th Edition)
/
Mac Secrets CD 4th Ed.toast
/
Apple Advanced Technologies
/
Apple Speech Technologies 1.5
/
PlainTalk Developer Info
/
Speech Recognition Manager SDK
/
SR Sample Code
/
IM SR Example
/
MyBuildLanguageModel.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-04-10
|
4KB
|
124 lines
#include <SpeechRecognition.h>
#include <string.h>
SRLanguageModel MyBuildLanguageModel (SRRecognitionSystem mySystem);
/* creates language model for the following BNF:
<TopLM> = <call> <person> | schedule meeting with <person> | view today's schedule;
<call> = call | phone | dial;
<person>= Arlo | Matt | Brent | my wife;
*/
const long kTopLMRefCon = 'top ';
const long kCallPersonRefCon = 'call';
const char kCallLMName[] = "<call>";
const char * kCallSynonyms[] = {"call", "phone", "dial", NULL};
const char kPersonLMName[] = "<person>";
const char * kPersonNames[] = {"Arlo", "Matt", "Brent", "my wife", NULL};
const char kTomLMName[] = "<TopLM>";
const char kScheduleMeetingWith[] = "schedule meeting with";
const char kViewTodaysSchedule[] = "view today's schedule";
SRLanguageModel MyBuildLanguageModel (SRRecognitionSystem mySystem)
{
OSErr myErr = noErr;
SRLanguageModel myCallLM = 0, myPersonLM = 0, myTopLM = 0;
SRPath myPath;
char ** myStringArray;
char * myCurrString;
// create an embedded language model named "<call>"
myErr = SRNewLanguageModel (mySystem, &myCallLM, kCallLMName, strlen(kCallLMName));
if (!myErr) {
SRPhrase myPhrase;
myStringArray = (char **) kCallSynonyms;
while ((myCurrString = *myStringArray) != NULL) {
/* note that we call SRNewPhrase instead of SRNewWord */
/* so that we don't have to know if any of the synonyms */
/* is more than one word */
myErr = SRNewPhrase (mySystem, &myPhrase, myCurrString, strlen(myCurrString));
if (!myErr) {
if (!myErr) myErr = SRAddLanguageObject (myCallLM, myPhrase);
SRReleaseObject (myPhrase); /* balances SRNewPhrase */
}
myStringArray++;
}
}
/* create an embedded language model named "<person>" */
/* Note that this code uses SRAddText, a useful shortcut */
/* which calls SRNewPhrase internally */
if (!myErr)
myErr = SRNewLanguageModel (mySystem, &myPersonLM, kPersonLMName,
strlen(kPersonLMName));
if (!myErr) {
long myRefCon = 0;
myStringArray = (char **) kPersonNames;
while ((myCurrString = *myStringArray) != NULL) {
myErr = SRAddText (myPersonLM, myCurrString, strlen(myCurrString),
myRefCon++);
/* Note, we set the refcon in the SRAddText call, so we */
/* can use it later when processing the search result */
myStringArray++;
}
}
/* create a top-level language model named "<TopLM>" */
if (!myErr)
myErr = SRNewLanguageModel (mySystem, &myTopLM, kTomLMName,
strlen(kTomLMName));
/* we set the refcon of the top-level language model, */
/* so that we can identify it in the search result */
if (!myErr) myErr = SRSetProperty (myTopLM, kSRRefCon, &kTopLMRefCon,
sizeof (kTopLMRefCon));
/* create a path for "<call> <person>" and add to "<TopLM>" */
if (!myErr) myErr = SRNewPath (mySystem, &myPath);
if (!myErr) {
if (!myErr) myErr = SRAddLanguageObject (myPath, myCallLM);
if (!myErr) myErr = SRAddLanguageObject (myPath, myPersonLM);
if (!myErr) myErr = SRAddLanguageObject (myTopLM, myPath);
/* we set the refcon of the path, so that we can identify */
/* it in the search result */
if (!myErr) myErr = SRSetProperty (myPath, kSRRefCon, &kCallPersonRefCon,
sizeof(kCallPersonRefCon));
SRReleaseObject (myPath); /* balances SRNewPath */
}
/* create a path for "schedule meeting with <person>" and add to "<TopLM>" */
if (!myErr) myErr = SRNewPath (mySystem, &myPath);
if (!myErr) {
if (!myErr) myErr = SRAddText (myPath, kScheduleMeetingWith,
strlen(kScheduleMeetingWith), 0);
if (!myErr) myErr = SRAddLanguageObject (myPath, myPersonLM);
if (!myErr) myErr = SRAddLanguageObject (myTopLM, myPath);
SRReleaseObject (myPath); /* balances SRNewPath */
}
/* add "view today's schedule" to "<TopLM>" */
if (!myErr) myErr = SRAddText (myTopLM, kViewTodaysSchedule,
strlen(kViewTodaysSchedule), 0);
if (myCallLM) SRReleaseObject (myCallLM); /* balances SRNewLanguageModel */
if (myPersonLM) SRReleaseObject (myPersonLM); /* balances SRNewLanguageModel */
return myTopLM;
}